package org.jsola.contract.service.impl;

import org.jsola.contract.common.ExampleKit;
import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dao.IVariableDAO;
import org.jsola.contract.dto.VariableAddDTO;
import org.jsola.contract.dto.VariableUpdateDTO;
import org.jsola.contract.entity.VariableDO;
import org.jsola.contract.query.VariableQuery;
import org.jsola.contract.service.IVariableService;
import org.jsola.contract.vo.VariableListVO;
import org.jsola.contract.vo.VariableVO;
import org.jsola.core.Page;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.jsola.user.constant.UserConstants.DEFAULT_SITE_ID;

/**
 * 模板变量
 *
 * @author JanusMix
 */
@Service("contractVariableServiceImpl")
public class VariableServiceImpl implements IVariableService {

    @Autowired
    private IVariableDAO variableDAO;

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public VariableVO save(VariableAddDTO variableAddDTO, TokenUser tokenUser) {
        // 转DO
        VariableDO variableDO = variableAddDTO.to(VariableDO.class);
        // 保存
        variableDO = save(variableDO, DEFAULT_SITE_ID, tokenUser.getUserId());
        return variableDO.to(VariableVO.class);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int update(VariableUpdateDTO variableUpdateDTO, TokenUser tokenUser) {
        //转DO
        VariableDO variableDO = variableUpdateDTO.to(VariableDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(variableDO, DEFAULT_SITE_ID, tokenUser.getUserId());
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... variableIds) {
        return logicalDeleteByIds(DEFAULT_SITE_ID, tokenUser.getUserId(), (Object[]) variableIds);
    }


    @Override
    public VariableVO selectById(Long variableId) {
        VariableDO variableDO = selectDOById(variableId);
        if (variableDO == null) {
            return null;
        }
        return variableDO.to(VariableVO.class);
    }

    @Override
    public List<VariableListVO> select(VariableQuery variableQuery) {
        List<VariableDO> variableDOList = selectDO(variableQuery);
        if(CollectionUtils.isEmpty(variableDOList)) {
            return variableDOList == null ? null : new ArrayList<>();
        }
        return variableDOList.stream()
                .map(variableDO -> variableDO.to(VariableListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<VariableDO> selectAll() {
        Example example = new Example(VariableDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", DEFAULT_SITE_ID);
        return variableDAO.selectByExample(example);
    }

    @Override
    public int selectCount(VariableQuery variableQuery) {
        Example example = buildExample(variableQuery);
        return variableDAO.selectCountByExample(example);
    }

    @Override
    public Page<VariableListVO> selectPage(VariableQuery variableQuery) {
        Example example = buildExample(variableQuery);
        Page<VariableDO> page = variableDAO.selectPageByExample(example,
                variableQuery.getPageNo(),
                variableQuery.getPageSize());

        return page.to(VariableListVO.class);
    }

    @Override
    public VariableDO selectDOById(Long variableId) {
        return listById(variableId, DEFAULT_SITE_ID);
    }

    @Override
    public List<VariableDO> selectDO(VariableQuery variableQuery) {
        Example example = buildExample(variableQuery);
        return variableDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param variableQuery 查询参数
     * @return example
     */
    private Example buildExample(VariableQuery variableQuery) {
        Example example = new Example(VariableDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", DEFAULT_SITE_ID);
        if (variableQuery.getName() != null) {
            example.and().andEqualTo("name", variableQuery.getName());
        }
        if (variableQuery.getType() != null) {
            example.and().andEqualTo("type", variableQuery.getType());
        }
        if (variableQuery.getNeed() != null) {
            example.and().andEqualTo("need", variableQuery.getNeed());
        }
        // 排序
        ExampleKit.setExampleOrder(example,variableQuery.getOrders());
        return example;
    }
}




